<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      function createNode(value) {
        // debugger
        return {
          value: value,
          children: [],
        };
      }
      function addNode(parentNode, childValue) {
        const childNode = createNode(childValue);
        parentNode.children.push(childNode);
      }
      function findNode(node, value) {
        if (node.value === value) return node;
        for (let child of node.children) {
          const result = findNode(child, value);
          //
          if (result) return result;
        }
        return null;
      }
      function traverse(node) {
        console.log(node.value); // 访问当前节点
        for (let child of node.children) {
          traverse(child); // 递归访问子节点
        }
      }

      // 示例用法
    //   const root = createNode("Root");
    //   addNode(root, "Child 1");
    //   addNode(root, "Child 2");

    //   addNode(findNode(root, "Child 1"), "Grandchild 1");

    //   console.log("Tree Traversal:");
    //   traverse(root);
    </script>
  </body>
</html>
